package com.ysd.lis.service.impl;

import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.WkTestBig;
import com.ysd.lis.entity.WkTestSmall;
import com.ysd.lis.mapper.WkTestBigMapper;
import com.ysd.lis.service.WkTestBigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.lis.service.WkTestSmallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 考试-大题分组表 服务实现类
 * </p>
 *
 * @author LiuQi
 * @since 2024-04-02
 */
@Service
public class WkTestBigServiceImpl extends ServiceImpl<WkTestBigMapper, WkTestBig> implements WkTestBigService {

    @Autowired
    WkTestBigMapper wkTestBigMapper;

    @Autowired
    WkTestSmallService wkTestSmallService;

    @Override
    public Result getTestBigSubjectList(String paperId) {
        MPJLambdaWrapper<WkTestBig> lambdaQueryWrapper = JoinWrappers.lambda(WkTestBig.class)
                .selectAll(WkTestBig.class)
                .selectCollection(WkTestSmall.class, WkTestBig::getTestSmallList)
                .leftJoin(WkTestSmall.class, WkTestSmall::getBigSubjectId, WkTestBig::getId)
                .eq(WkTestBig::getDelFlag, 0)
                .eq(WkTestBig::getPaperId, paperId)
                .orderByAsc(WkTestBig::getSeq)
                .orderByAsc(WkTestSmall::getSeq);
        List<WkTestBig> wkTestBigs = wkTestBigMapper.selectJoinList(WkTestBig.class, lambdaQueryWrapper);
        return Result.succ(1, "查询成功", wkTestBigs);
    }

    @Override
    public Result addTestBigSubject(WkTestBig wkTestBig) {
        boolean save = this.save(wkTestBig);
        if (save) {
            return Result.succ(1, "添加成功", save);
        } else {
            return Result.fail("添加失败");
        }
    }

    @Override
    public Result editTestBigSubject(WkTestBig wkTestBig) {
        boolean save = this.updateById(wkTestBig);
        if (save) {
            return Result.succ(1, "编辑成功", save);
        } else {
            return Result.fail("编辑失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result deleteTestBigSubject(WkTestBig wkTestBig) {
        boolean delete = this.removeById(wkTestBig.getId());
        delete = delete && wkTestSmallService.clearQuestionAndOptionByBigId(wkTestBig.getId());
        if (delete) {
            return Result.succ(1, "删除成功", delete);
        } else {
            return Result.fail("删除失败");
        }
    }
}
